package com.sduept.nwld.dataserver.manager.protection;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sduept.bigdata.document.entity.core.RecordAttachment;
import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.bigdata.document.core.CommonDocumentProperties;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.daily.SpecialReport;
import com.sduept.nwld.dataserver.model.daily.UrlFile;
import com.sduept.utils.DateUtil;

//import com.sduept.bigdata.fault.common.ParamDesc;

@Service
public class SpecialReportManager {
	@Autowired
	private SuperDAO dao;
	@Autowired
	private ParamManager pc;
	@Autowired
	private CommonDocumentProperties pros;
	public List<SpecialReport> getSpecialReport(String start, String end) {
		Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymdhms);
		Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymdhms);
		return dao.findByCondition(SpecialReport.class, " day between ? and ? order by day desc", s, e);
	}

	/**
	 * 数据更新
	 */
	public void updateSpecialReport(SpecialReport selected) {
		if (selected != null) {
			try {
				dao.update(selected, SpecialReport.class); // update新的数据到这条Overhaulmag
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 新增数据
	 * 
	 * @return
	 */
	public SpecialReport creatSpecialReport(SpecialReport created) {
		if (created != null) {
			try {
				created = dao.create(created, SpecialReport.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return created;
	}

	/**
	 * 单条删除
	 */
	public void deleteSpecialReport(SpecialReport selectReport) {
		if (selectReport != null) {
			try {
				dao.delete(selectReport.getId(), SpecialReport.class);
				deleteFiles(selectReport.getId());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 根据专项汇报id（附件的oid）批量删除附件
	 * 
	 * @param oid
	 */
	public void deleteFiles(String oid) {
		List<RecordAttachment> files = dao.findByCondition(RecordAttachment.class, " oid = ?", oid);
		for (RecordAttachment f : files) {
			dao.delete(f.getId(), RecordAttachment.class);
			removeFile(oid, f);
		}
	}

	public void removeFile(String oid, RecordAttachment record) {
		try {
			String removePath = pros.getDocumentPath() + record.getName();
			File f = new File(removePath);
			if (f.exists()) {
				f.delete();
				dao.delete(record.getId(), RecordAttachment.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<SpecialReport> getReportWithUrls(List<SpecialReport> reports) {
		for (int i = 0; i < reports.size(); i++) {
			SpecialReport p = reports.get(i);
			if (StringUtils.isNotEmpty(p.getFileSaveName())) {
				String[] urlNames = p.getFileSaveName().split(";");
				for (int j = 0; j < urlNames.length; j++) {
					String name = urlNames[j];
					if (StringUtils.isNotEmpty(name)) {
						UrlFile file = new UrlFile();
						file.setName(name);
						file.setUrl(getUrlByFileName(urlNames[j]));
						p.getUrlNames().add(file);
					}
				}
			} else {
				continue;
			}
		}
		return reports;
	}

	/**
	 * 通过fileName获得url信息
	 * 
	 * @param fileName
	 * @return
	 */
	public String getUrlByFileName(String fileName) {
		if (StringUtils.isNotEmpty(fileName)) {
			String before = pc.get(ParamDesc.OMS_SPECIAL_FILE_URL).getValue();
			String name = "";
			try {
				name = URLEncoder.encode(fileName, "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return before + name;
		}
		return null;
	}

	/**
	 * 通过fileName获得文件列表
	 */
	public List<UrlFile> getUrlListByFileNames(String fileNames) {
		List<UrlFile> Urlfiles = new ArrayList<>();
		if (StringUtils.isEmpty(fileNames)) {
		} else {
			String[] fileName = fileNames.split(";");
			for (int i = 0; i < fileName.length; i++) {
				if (StringUtils.isNotEmpty(fileName[i])) {
					String fileUrl = getUrlByFileName(fileName[i]);
					UrlFile f = new UrlFile();
					f.setName(fileName[i]);
					f.setUrl(fileUrl);
					Urlfiles.add(i, f);
				}
			}
		}
		return Urlfiles;
	}

	/**
	 * 通过oid获得文件列表
	 * 
	 * @param oid
	 * @return
	 */
	public List<RecordAttachment> findFileByOId(String oid) {
		return dao.findByCondition(RecordAttachment.class, "oid=? order by description,uploadDate desc", oid);
	}

	/**
	 * 通过id获得专项汇报
	 * 
	 * @param id
	 * @return
	 */
	public SpecialReport findReportById(String id) {
		return dao.findById(id, SpecialReport.class);
	}

	/**
	 * 根据文件id单条删除文件
	 * 
	 * @param fileId
	 */
	public void deleteFile(String fileId) {
		dao.delete(fileId, RecordAttachment.class);
	}

	/**
	 * 根据附件id获取附件
	 * 
	 * @param id
	 * @return
	 */
	public RecordAttachment findFileById(String id) {
		return dao.findById(id, RecordAttachment.class);
	}
	
	public RecordAttachment createOrUpdateFile(RecordAttachment record) {
		return dao.createOrUpdate(record, RecordAttachment.class);
	}

}
